﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DAL;

namespace BLL
{
    public class BRole
    {
        private readonly DAL.DRole _dRole = new DRole();
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public  int GetMaxId()
        {
            return _dRole.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string roleName)
        {
            return _dRole.Exists(roleName);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public  bool Add(Model.RoleInfo model)
        {
            return _dRole.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(int id, string name, string permission, string remark)
        {
            return _dRole.Update(id, name, permission, remark);
        }
        /// <summary>
        /// 由用户名得到该用户所属分组及权限
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
         public string[] GetRoleInfoByUserName(string username)
        {
            return _dRole.GetRoleInfoByUserName(username);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public  bool Delete(int id)
        {

            return _dRole.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public  bool DeleteList(string idlist)
        {
            return _dRole.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public  Model.RoleInfo GetModel(int id)
        {

            return _dRole.GetModel(id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  DataSet GetList(string strWhere)
        {
            return _dRole.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  List<Model.RoleInfo> GetModelList(string strWhere)
        {
            DataSet ds = _dRole.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  List<Model.RoleInfo> DataTableToList(DataTable dt)
        {
            List<Model.RoleInfo> modelList = new List<Model.RoleInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.RoleInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.RoleInfo();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["RoleName"] != null && dt.Rows[n]["RoleName"].ToString() != "")
                    {
                        model.RoleName = dt.Rows[n]["RoleName"].ToString();
                    }
                    if (dt.Rows[n]["Permission"] != null && dt.Rows[n]["Permission"].ToString() != "")
                    {
                        model.Permission = dt.Rows[n]["Permission"].ToString();
                    }
                    if (dt.Rows[n]["Remark"] != null && dt.Rows[n]["Remark"].ToString() != "")
                    {
                        model.Remark = dt.Rows[n]["Remark"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public  int GetRecordCount(string strWhere)
        {
            return _dRole.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public  DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return _dRole.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        #endregion  Method
    }
}
